package com.gzx.qc.modular.check.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gzx.qc.modular.check.entity.GzxCallCheck;
import com.gzx.qc.modular.check.mapper.GzxCallCheckMapper;
import com.gzx.qc.modular.check.service.GzxCallCheckDbService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/7/29 18:06
 */
@DS("qc")
@Service
@RequiredArgsConstructor
public class GzxCallCheckDbServiceImpl implements GzxCallCheckDbService {

    private final GzxCallCheckMapper callCheckMapper;

    /**
     * 查询质检任务
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    public List<GzxCallCheck> selectList(LambdaQueryWrapper<GzxCallCheck> lambdaQueryWrapper) {
        return callCheckMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据ID查询
     *
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    public GzxCallCheck selectOne(LambdaQueryWrapper<GzxCallCheck> lambdaQueryWrapper) {
        return callCheckMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 批量保存
     *
     * @param callCheckList
     */
    @Override
    public void insertBatch(List<GzxCallCheck> callCheckList) {
        String sql = callCheckList.stream()
                .map(s -> String.format("(%s,'%s',%s,'%s',%s,'%s','%s',%s,'%s','%s')",
                        escapeSql(IdUtil.getSnowflakeNextIdStr()),
                        escapeSql(s.getCallId()),
                        escapeSql(String.valueOf(s.getCallType())),
                        escapeSql(s.getCallee()),
                        escapeSql(String.valueOf(s.getCallDuration())),
                        escapeSql(s.getAgentNum()),
                        escapeSql(s.getUserName()),
                        escapeSql(String.valueOf(s.getDeptId())),
                        escapeSql(s.getFileUrl()),
                        escapeSql(s.getKeyword())))
                .collect(Collectors.joining(", ")).replace("'null'", "null");
        callCheckMapper.insertBatch(sql);
    }

    /**
     * 根据ID修改
     *
     * @param callCheck
     */
    @Override
    public void updateById(GzxCallCheck callCheck) {
        callCheckMapper.updateById(callCheck);
    }

    /**
     * 修改
     *
     * @param lambdaUpdateWrapper
     */
    @Override
    public void updateByWrapper(LambdaUpdateWrapper<GzxCallCheck> lambdaUpdateWrapper) {
        callCheckMapper.update(lambdaUpdateWrapper);
    }

    /**
     * 补全关键词
     *
     * @param callCheckList
     */
    @Override
    public void completionCheckKeyword(List<GzxCallCheck> callCheckList) {
        callCheckMapper.completionCheckKeyword(callCheckList);
    }

    /**
     * 修改AI审核状态
     *
     * @param callCheckList
     */
    @Override
    public void modifyAiReviewStatus(List<GzxCallCheck> callCheckList) {
        callCheckMapper.modifyAiReviewStatus(callCheckList);
    }
}
